﻿
#include "Application.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QApplication>
#include <QProcess>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickWindow>
#include <QSslConfiguration>
#include <QtQml/qqmlextensionplugin.h>

#include "global/LinkManager.h"
#include "global/SettingsHelper.h"
#include "global/ControllerManager.h"
#include "controllers/SerialDebugger.h"

#include "fact/Fact.h"
#include "fact/FactGroup.h"

const char * RefOnly = "Reference only";

Application* Application::app_ = nullptr;
Application* app()
{
    return Application::app_;
}

Application::Application(int &argc, char* argv[])
    : QApplication          (argc, argv)
{
    app_ = this;
}

void Application::init(QQmlApplicationEngine *engine)
{
    engine_ = engine;
    QString language = SettingsHelper::Instance()->getLanguage();
    const QString baseName = "gwzd-device-tuner_" + QLocale(language).name();
    if (translator_.load("./i18n/"+ baseName)) {
        this->installTranslator(&translator_);
        engine_->retranslate();
    }
    initCommon_();
    registerType_();
}

QQmlApplicationEngine * Application::engine()
{
    return this->engine_;
}

void Application::setLanguage(int language)
{
    if(language){
        const QString baseName = "gwzd-device-tuner_" + QLocale("en_US").name();
        if (translator_.load("./i18n/"+ baseName)) {
            this->installTranslator(&translator_);
            engine_->retranslate();
        }
    }
    else {
        const QString baseName = "gwzd-device-tuner_" + QLocale("zh_CN").name();
        if (translator_.load("./i18n/"+ baseName)) {
            this->installTranslator(&translator_);
            engine_->retranslate();
        }
    }
}

void Application::initCommon_()
{

}

void Application::registerType_()
{
    //global
    engine_->rootContext()->setContextProperty          ("SettingsHelper",SettingsHelper::Instance());
    engine_->rootContext()->setContextProperty          ("LinkManager",LinkManager::Instance());
    engine_->rootContext()->setContextProperty          ("ControllerManager",ControllerManager::Instance());
    //comm
    qmlRegisterUncreatableType<LinkInterface>           ("App",1,0,"LinkInterface",RefOnly);
    qmlRegisterUncreatableType<SerialLink>              ("App",1,0,"SerialLink",RefOnly);
    qmlRegisterUncreatableType<ModbusRtuLink>           ("App",1,0,"ModbusRtuLink",RefOnly);
    //controllers
    qmlRegisterUncreatableType<TowerDevicesController>  ("App",1,0,"TowerDevicesController",RefOnly);
    qmlRegisterUncreatableType<Fact>                    ("App",1,0,"Fact",RefOnly);
    qmlRegisterUncreatableType<FactGroup>               ("App",1,0,"FactGroup",RefOnly);
    qmlRegisterUncreatableType<LineTempFactGroup>       ("App",1,0,"LineTempFactGroup",RefOnly);
    qmlRegisterUncreatableType<GroundedFactGroup>       ("App",1,0,"GroundedFactGroup",RefOnly);
    qmlRegisterUncreatableType<SerialDebugger>          ("App",1,0,"SerialDebugger",RefOnly);
}
